/**
 *
 * MTG Life Counter
 * Manages themes of background, font, etc.
 *
 * Created by Nathan Black 
 */

#import <CoreFoundation/CoreFoundation.h>
#import "ThemeManager.h"

static ThemeManager *s_themeMgrInstance;

@interface ThemeManager (Private)

- (void) loadDefaults:(NSString*)homeDir;

@end


@implementation LifeCounterTheme

- (NSString*)backgroundFile
{
    return _backgroundFile;
}

- (void)setBackgroundFile:(NSString*)newFile
{
    //if (_backgroundFile != NULL)
        [_backgroundFile release];
        
    _backgroundFile = newFile;
    [_backgroundFile retain];
}

- (struct __GSFont*)font
{
    return _font;
}

- (void)setFont:(struct __GSFont*)newFont
{
    _font = newFont;
}

- (void)dealloc
{
    [_backgroundFile dealloc];
    [super dealloc];
}

@end

@implementation ThemeManager

+ (ThemeManager *)globalInstance
{
    if (s_themeMgrInstance == NULL) {
        s_themeMgrInstance = [[[ThemeManager alloc] init] retain];
    }
    return s_themeMgrInstance;
}

-init 
{
    if ((self = [super init])) {
        _themes = [[NSMutableArray alloc] init];
        [self loadDefaults:@"."];
    }
    return self;
}

- (void)dealloc
{
    [_themes release];
    [super dealloc];
}

- (void) loadDefaults:(NSString*)homeDir
{
    LifeCounterTheme* theme;
    
    theme = [[[LifeCounterTheme alloc] init] autorelease];
    [theme setBackgroundFile:@"Default.png"];
    [theme setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Georgia" traits:2 size:132.0f]];
    [_themes addObject:theme];

    theme = [[[LifeCounterTheme alloc] init] autorelease];
    [theme setBackgroundFile:[homeDir stringByAppendingString:@"/images/Evacuation.png"]];
    [theme setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Verdana" traits:2 size:96.0f]];
    [_themes addObject:theme];

    theme = [[[LifeCounterTheme alloc] init] autorelease];
    [theme setBackgroundFile:[homeDir stringByAppendingString:@"/images/Ashling.png"]];
    [theme setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Georgia" traits:2 size:132.0f]];
    [_themes addObject:theme];

    theme = [[[LifeCounterTheme alloc] init] autorelease];
    [theme setBackgroundFile:[homeDir stringByAppendingString:@"/images/ShadowOfDoubt.png"]];
    [theme setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Verdana" traits:2 size:96.0f]];
    [_themes addObject:theme];

    theme = [[[LifeCounterTheme alloc] init] autorelease];
    [theme setBackgroundFile:[homeDir stringByAppendingString:@"/images/Flamekin.png"]];
    [theme setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Georgia" traits:2 size:132.0f]];
    [_themes addObject:theme];
}

- (int)currTheme
{
    return _currThemeIdx;
}

- (void)nextTheme
{
    if (_currThemeIdx < [_themes count] - 1) {
        _currThemeIdx++;
    }
    else {
        _currThemeIdx = 0;
    }
}

- (void)prevTheme
{
    if (_currThemeIdx > 0) {
        _currThemeIdx--;
    }
    else {
        _currThemeIdx = [_themes count] - 1;
    }
}

- (void)setTheme:(int)themeIdx
{
    if (themeIdx >= 0 && themeIdx < [_themes count])
        _currThemeIdx = themeIdx;
}

- (NSString*)currBackgroundFile
{
    return [[_themes objectAtIndex:_currThemeIdx] backgroundFile];
}

- (struct __GSFont*)currFont
{
    return [[_themes objectAtIndex:_currThemeIdx] font];
}


@end